package com.object.module.lq.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.object.dao.sys.TRoleAuthorizationDao;
import com.object.module.lq.sys.service.TRoleAuthorizationService;
import com.object.module.lq.sys.service.TRoutingService;
import com.object.module.lq.sys.service.TUserService;
import com.object.module.lq.sys.entity.TRoleAuthorizationEntity;
import com.object.module.lq.sys.entity.TRoleEntity;
import com.object.module.lq.sys.entity.TUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TRoleAuthorizationServiceImpl extends ServiceImpl<TRoleAuthorizationDao, TRoleAuthorizationEntity> implements TRoleAuthorizationService {

    @Autowired
    private TRoutingService routingService;

    @Autowired
    private TUserService userService;

    @Override
    public List<String> queryPermissionsBasedOnUserId(Integer userId) {
        List<String> permissions = new ArrayList<>();
        TUserEntity userEntity = userService.getById(userId);
        TRoleEntity role = userEntity.getRole();
        Integer reId = role.getReId();
        QueryWrapper<TRoleAuthorizationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("re_id", reId);
        List<TRoleAuthorizationEntity> list = list(wrapper);
        list.forEach(item -> {
            Integer rtId = item.getRtId();
            String path = routingService.getById(rtId).getPath();
            permissions.add(path + "_" + item.getRaPermission());
        });
        return permissions;
    }
}
